package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Util.JsonUtil;
import dao.AreasDao;
import daoImpl.AreasDaoImpl;
import pojo.Areas;

/**
 * Servlet implementation class SearchServlet
 */
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public SearchServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;chartset=utf-8");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		String k = request.getParameter("key");
		System.out.println(k);
		String key = java.net.URLDecoder.decode(k,"utf-8");
		System.out.println("收到的key值为：" + key);
		AreasDao areasDao = new AreasDaoImpl();
		List<Areas> list =null;
		list = areasDao.findByKey(key);
		
		if(list != null){
			String json = JsonUtil.listToJson(list);
			System.out.println(json);
			PrintWriter out = response.getWriter();
			out.write(json);		
		}
		
	}

}
